﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Linq;
using System.Collections.Generic;
using System.IO;

namespace Winnow.SharepointDesigner.Layouts.Winnow.SharepointDesigner
{
    public partial class SharepointExplorer : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    LoadTreeView();
                    hlnkHomeUrl.NavigateUrl = SPContext.Current.Site.Url;
                    hlnkHomeUrl.Text = SPContext.Current.Site.Url;
                    FillLog();
                }
            }
            catch (Exception ex)
            {

                Response.Write(ex.Message);
            }

        }

        private void FillLog()
        {
            grdLog.DataSource = GetLOGSFile();
            grdLog.AutoGenerateColumns = false;
            grdLog.DataBind();
        }
        private void LoadTreeView()
        {

            using (SPSite ospsite = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb ospweb = ospsite.OpenWeb())
                {
                    lblOwner.Text = ospsite.Owner.LoginName;
                    lblDescription.Text = ospweb.Description;
                    lblTitle.Text = ospweb.Title;
                    if (ospweb.SiteLogoUrl != null)
                    {
                        imgLogo.ImageUrl = ospweb.SiteLogoUrl;
                    }
                    else
                    {
                        imgLogo.ImageUrl = "/_layouts/images/SharePointFoundation16.png";
                    }

                    TreeNode vsNode = new TreeNode("<img  src='/_layouts/images/SharePointFoundation16.png' /><span>" + ospweb.Title + "</span>");
                    vsNode.NavigateUrl = "javascript:addWebTab('" + ospweb.ID.ToString() + "','" + ospweb.Title + "')";
                    treSites.Nodes.Add(vsNode);

                    SPWebCollection ospWebColl = ospweb.GetSubwebsForCurrentUser();
                    GetAllSubWebs(ospWebColl, vsNode);

                    foreach (SPList item in ospweb.Lists)
                    {
                        TreeNode vsNode1 = new TreeNode("<img src='" + item.ImageUrl + "' /><span>" + item.Title + "</span>");
                        vsNode1.NavigateUrl = "javascript:addListTab('" + ospweb.ID.ToString() + "','" + item.ID.ToString() + "','" + item.Title + "','" + item.ImageUrl + "')";
                        vsNode.ChildNodes.Add(vsNode1);
                    }
                }
            }

        }

        private void GetAllSubWebs(SPWebCollection spCollection, TreeNode vsNode)
        {
            foreach (SPWeb webItem in spCollection)
            {

                TreeNode vsNode1 = new TreeNode("<img  src='/_layouts/images/SharePointFoundation16.png' /><span>" + webItem.Name + "</span>");
                vsNode1.NavigateUrl = "javascript:addWebTab('" + webItem.ID.ToString() + "','" + webItem.Title + "')";

                vsNode.ChildNodes.Add(vsNode1);


                if (webItem.Webs.Count != 0)
                {
                    GetAllSubWebs(webItem.Webs, vsNode1);
                }

                foreach (SPList item in webItem.Lists)
                {

                    TreeNode listItem = new TreeNode("<img src='" + item.ImageUrl + "' /><span>" + item.Title + "</span>");
                    listItem.NavigateUrl = "javascript:addListTab('" + webItem.ID.ToString() + "','" + item.ID.ToString() + "','" + item.Title + "','" + item.ImageUrl + "')";
                    vsNode1.ChildNodes.Add(listItem);
                }
            }
        }
        string defaultLOGSPath = @"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\LOGS";
        public List<KeyValuePair<string, string>> GetLOGSFile()
        {
            KeyValuePair<string, string> file;
            List<KeyValuePair<string, string>> files = new List<KeyValuePair<string, string>>();

            DirectoryInfo directory = new DirectoryInfo(defaultLOGSPath);
            if (!directory.Exists)
            {
                throw new Exception("Directory " + defaultLOGSPath + " not exist!");
            }
            else
            {
                foreach (var item in directory.GetFiles("*.log").OrderByDescending(d => d.LastWriteTime))
                {
                    file = new KeyValuePair<string, string>(item.FullName, item.Name);
                    files.Add(file);
                }
            }

            return files;

        }
    }
}
